package com.fkcai.hansoul.controller;

import com.fkcai.hansoul.common.util.Result;
import com.fkcai.hansoul.common.exception.ComFoundException;
import com.fkcai.hansoul.common.util.ResultCode;
import com.fkcai.hansoul.service.FileService;
import com.fkcai.hansoul.common.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件控制器类：负责文件操作
 */
@RestController
@RequestMapping("/api/files")
public class FileController {
	@Autowired
	private FileService fileService;
	
	
	/**
	 * 文件上传接口
	 * @param file 文件
	 * @param fileType 文件类型
	 * @return 文件上传后的路径和信息
	 */
	@PostMapping("/upload")
	public Result<String> uploadFile (
			@RequestParam("file") MultipartFile file,
			@RequestParam(value = "type", defaultValue = "image") String fileType) {

			// 检验文件类型
			if (!FileUtil.validateFileType(file, fileType)) {
				throw new ComFoundException(ResultCode.FILE_FORMAT_ERROR);
			}
			
			String fileUrl = fileService.uploadFile(file, fileType);
			
			return Result.success(fileUrl);
	}
	
}
